<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : changecode.class.php   FILE_PATH : classes\libraries\changecode.class.php
 * ....简体繁体中文互相转换
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @version Wed Apr 16 05:32:57 CST 2008
 */
@header("content-type:text/html;charset=utf-8");
class ChangeCode{

	/**
	 * 简体中文
	 *
	 * @var int
	 */
	var $gb;

	/**
	 * 繁体中文
	 *
	 * @var int
	 */
	var $big5;


	/**
	 * 将繁体中文转换为简体中文
	 *
	 * @param string $Text
	 */
	function big52gb($Text) {
		$filename = BasePath."/classes/libraries/encode/big5-gb.table";
		$fp = fopen($filename, "rb");
		$big5 = fread($fp,filesize($filename));
		fclose($fp);
		$Text = iconv("utf-8","big5",$Text);
		$max = strlen($Text)-1;
		for($i=0;$i<$max;$i++) {
			$h = ord($Text[$i]);
			if($h>=160) {
				$l = ord($Text[$i+1]);
				if($h==161 && $l==64) {
					$gb = "　";
				}else{
					$p = ($h-160)*510+($l-1)*2;
					$gb = $big5[$p].$big5[$p+1];
				}
				$Text[$i] = $gb[0];
				$Text[$i+1] = $gb[1];
				$i++;
			}
		}
		return iconv("gb2312","utf-8",$Text);
	}

	/**
	 * 将简体中文转换为繁体中文
	 *
	 * @param string $Text
	 */	

	function gb2big5($Text) {
		$filename = BasePath."/classes/libraries/encode/gb-big5.table";
		$fp = fopen($filename, "rb");
		$gb = fread($fp,filesize($filename));
		fclose($fp);
		$Text = iconv("utf-8","gb2312",$Text);
		$max = strlen($Text)-1;
		for($i=0;$i<$max;$i++) {
			$h = ord($Text[$i]);
			if($h>=160) {
				$l = ord($Text[$i+1]);
				if($h==161 && $l==64) {
					$big = "　";
				}else{
					$p = ($h-160)*510+($l-1)*2;
					$big = $gb[$p].$gb[$p+1];
				}
				$Text[$i] = $big[0];
				$Text[$i+1] = $big[1];
				$i++;
			}
		}
		return iconv("big5","utf-8",$Text);
	}

}
?>